<template>
	<sw-form-frame v-model="visible" :title="formName" width="560px" :type="formType">
		<el-form ref="form" size="medium" :model="formData" :rules="formRules" label-width="90px" v-loading="loading">
			<el-form-item label="上级院系" prop="pid">
				<sw-tree-select v-model="formData.pid" :list="departTree" v-bind="treeSelectConfig" :disabled="isDisabled"></sw-tree-select>
			</el-form-item>
			<el-form-item label="院系名称" prop="name">
				<el-input v-model="formData.name"></el-input>
			</el-form-item>
			<el-form-item label="院系描述">
				<el-input type="textarea" v-model="formData.content"></el-input>
			</el-form-item>
			<el-row>
				<el-col :span="15">
					<el-form-item label="是否使用" prop="del">
						<el-radio-group v-model="formData.del">
							<el-radio label="1">有效</el-radio>
							<el-radio label="2">无效</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="9">
					<el-form-item label="排序" label-width="60px">
						<el-input-number v-model="formData.sort" controls-position="right" :min="1" :max="999" style="width: 100%"></el-input-number>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template slot="footer">
			<el-button size="medium" type="primary" @click="handleSubmitForm()">新增</el-button>
			<el-button size="medium" @click="visible = false">取消</el-button>
		</template>
	</sw-form-frame>
</template>

<script>
import lodash from 'lodash';
import formMixins from '@/mixins/formMixins';
import { queryDepart, addedDepart, updateDepart } from '@/api/system/tbSysDept';
export default {
	mixins: [formMixins],
	props: {
		list: { type: Array, default: () => [] }
	},
	data() {
		return {
			formName: '院系',
			formRules: {
				pid: [{ required: true, message: '请选择上级院系', trigger: 'change' }],
				name: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
				del: [{ required: true, message: '请选择字典类型', trigger: 'change' }]
			},
			treeSelectConfig: {
				filterable: true,
				style: { width: '100%' },
				placeholder: '选择标签树',
				defaultProps: { label: 'name', children: 'childList' }
			}
		};
	},
	computed: {
		departTree() {
			let arr = [];
			if (this.addOrEdit || this.isSub) {
				arr = [...this.list];
			} else {
				arr = this.$removeTreeNode(this.list, this.dataId, 'childList');
			}
			console.log(arr);
			return [{ id: '0', name: '顶级院系', childList: arr }];
		},
		isDisabled() {
			return this.isSub;
		},
		detailData() {
			return eval(queryDepart);
		},
		callFunction() {
			if (this.addOrEdit) {
				return eval(addedDepart);
			} else if (this.isSub) {
				return eval(addedDepart);
			} else {
				return eval(updateDepart);
			}
		},
		submitParams() {
			let params = { ...this.formData };
			if (this.isSub) delete params.id;
			return params;
		}
	},
	methods: {}
};
</script>
